Ext.ns('WaterHistoryRecordView');

WaterHistoryRecordView = Ext.extend(Ext.Panel, {
	// 头部工具栏
	topbar : null,
	// 条件搜索Panel
	searchPanel : null,
	// 数据展示Panel
	gridPanel : null,
	// GridPanel的数据Store
	store : null,
	// 构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		// 初始化组件
		this.initUIComponents();
		// 调用父类构造
		WaterHistoryRecordView.superclass.constructor.call(this, {
					id : 'WaterHistoryRecordView',
					title : '水表记录查询',
					region : 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});

	},// end of constructor

	// 初始化组件
	initUIComponents : function() {
		// 初始化搜索条件Panel
		this.searchPanel = new Ext.FormPanel({
					id : 'WaterHistoryRecordSearchForm',
					height :120,
					region : 'north',
					frame : false,
					border : false,
					padding : '10',
					layout : 'column',
					title : '查询条件',
					labelWidth : 60,
					fileUpload : true,
					layoutConfig : {
						padding : '5',
						align : 'middle'
					},
					defaults : {
						columnWidth : 0.5,
						layout : 'form',
						border : false,
						style : 'padding-left:10px;',
						defaults : {
							xtype : 'textfield',
							width : 130
						}
					},
					items : [{
								items : [{
											fieldLabel : '水表编号',
											id : 'pdaWaterMeterRecord.wmId',
											name : 'Q_amId_S_LK'
										},{
											fieldLabel : '栋',
											hiddenName : 'Q_buildingId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterRecord.buildingID',
											valueField : 'buildingId',
											displayField : 'buildingtName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId=',
														//autoLoad : true,
														fields : ['buildingId','buildingtName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
													var h = Ext.getCmp('pdaWaterMeterRecord.houseID');
													h.setValue('');
													h.store.loadData([]);
													chooseBulidingId = cbo.getValue();
													Ext.Ajax.request({
														url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId='
																+ chooseBulidingId,
														method : 'post',
														success : function(response,option) {
															h.store.loadData(eval("("+ response.responseText+ ")"));
														}
													});
												}
											}
										
										
										
										},{
											fieldLabel : '房',
											width : 220,
											hiddenName : 'Q_aparmentId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterRecord.houseID',
											valueField : 'houseId',
											displayField : 'houseName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath+ '/pdaweb/showHouseWaterMeterPlan.do?buildingId=',
														//autoLoad : true,
														fields : ['houseId','houseName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
												}
											}
										
										
										}]
							},{
								items : [{
											fieldLabel : '区',
											hiddenName : 'Q_districtId_S_LK',
											xtype : 'combo',
											id : 'pdaWaterMeterRecord.districtID',
											valueField : 'districtId',
											displayField : 'districtName',
											mode : 'local',
											editable : false,
											triggerAction : 'all',
											store : new Ext.data.SimpleStore({
														url : __ctxPath
																+ '/pdaweb/showDistrictAmPlan.do',
														autoLoad : true,
														fields : ['districtId','districtName']
													}),
											listeners : {
												scope : this,
												select : function(cbo,record, index) {
													var b = Ext.getCmp('pdaWaterMeterRecord.buildingID');
													var h = Ext.getCmp('pdaWaterMeterRecord.houseID');
													b.setValue('');
													b.store.loadData([]);
													h.setValue('');
													h.store.loadData([]);
													
													chooseDistrictId = cbo.getValue();
													Ext.Ajax.request({
														url : __ctxPath+ '/pdaweb/showBuildingAmPlan.do?districtId='
																+ chooseDistrictId,
														method : 'post',
														success : function(response,option) {
															b.store.loadData(eval("("+ response.responseText
																			+ ")"));
														}
													});
												}
											}
										},{
												xtype : 'button',
												text : '查询',
												region : 'center',
												width : 60,
												iconCls : 'search',
												style : 'margin-center:10px',
												scope : this,
												handler : function(){
													var searchPanel = Ext.getCmp('WaterHistoryRecordSearchForm');
													var gridPanel = Ext.getCmp('WaterHistoryRecordGrid');
													if (searchPanel.getForm().isValid()) {
														$search({
															searchPanel : searchPanel,
															gridPanel : gridPanel
														});
													}
												}
											},{
												style : 'margin-top:4px;',
												width : 60,
						    					xtype : 'button',
						    					text : '重置',
						    					iconCls : 'btn-reset',
						    					handler : function() {
						    						Ext.getCmp('pdaWaterMeterRecord.wmId').setValue('');
						    						Ext.getCmp('pdaWaterMeterRecord.districtID').setValue('');
						    						Ext.getCmp('pdaWaterMeterRecord.buildingID').setValue('');
						    						Ext.getCmp('pdaWaterMeterRecord.houseID').setValue('');
						    						//Ext.getCmp('WaterHistoryRecordSearchForm').getForm().reset();
						    					}
											}]
							}]
				});// end of the searchPanel

		this.store = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : __ctxPath + '/pdaweb/listPdaWaterMeter.do',
								params : {
									start : 0,
									limit : 15
									}
							}),
					reader : new Ext.data.JsonReader({
								root : 'result',
								totalProperty : 'totalCounts',
								fields : [{
											name : 'id',
											type : 'int'
										}, 'type', 'amId','districtId',
										'buildingId','aparmentId','recorderId']
							}),
					remoteSort : true
				});
		this.store.load({
			params : {
				start : 0,
				limit : 15
			}
		});// end of the store

		var cm = new Ext.grid.ColumnModel({
					columns : [ new Ext.grid.RowNumberer(), {
								header : '水表编号',
								width : 120,
								dataIndex : 'amId',
								renderer : linkDetail
							}, {
								header : '区号',
								width : 80,
								dataIndex : 'districtId'
							}, {
								header : '栋号',
								width : 120,
								dataIndex : 'buildingId'
							}, {
								header : '房号',
								width : 180,
								dataIndex : 'aparmentId'
							}, {
								header : '状态',
								width : 90,
								dataIndex : 'type',
								renderer : function(val){
									if(val=='1'){
										return '<span style="color:#336600;">申请中</span>';
									}else if(val=='2'){
										return '使用中';
									}else if(val=='3'){
										return '<span style="color:#aaaaaa;">已报废</span>';
									}else if(val=='4'){
										return '<span style="color:red;">停用中</span>';
									}
									return '';
								}
							}],
					defaults : {
						sortable : true,
						menuDisabled : false
						//width : 100
					}
				});// end of the cm

		this.topbar = new Ext.Toolbar({
					id : 'WaterHistorRecordFootBar',
					height : 25,
					bodyStyle : 'text-align:left',
					items : []
				});//end of the topbar

		this.gridPanel = new Ext.grid.GridPanel({
					id : 'WaterHistoryRecordGrid',
					tbar : this.topbar,
					region : 'center',
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					autoHeight : true,
					cm : cm,
					bbar : new Ext.PagingToolbar({
								pageSize : 15,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});//end of the gridPanel

		var gridPanel = this.gridPanel;
	}
});
//弹出水表查询界面
WaterHistoryRecordView.detail = function(recId, quId, dongId, fangId, type) {
	new WaterHistoryRecordForm({
			recId : recId,
			quId  : quId,
			dongId : dongId,
			fangId : fangId,
			type : type
		}).show();
};

function linkDetail(value, cellmeta, record, rowIndex) {
	return "<a href='#' onClick=WaterHistoryRecordView.detail('" + value
			+ "'," + "'" + record.data["districtId"] + "','"
			+ record.data["buildingId"] + "'" + ",'"
			+ record.data["aparmentId"] + "','"+record.data["type"]+"')>" + value + "</a>";
	};



// 编辑
WaterHistoryRecordView.edit = function(recId, recordStr) {
	new WaterForm({
				recId : recId,
				recordStr : recordStr
			}).show();
};

